package cn.edu.sdjzu.xg.bysj.dao;

import cn.edu.sdjzu.xg.bysj.domain.*;

import cn.edu.sdjzu.xg.bysj.service.DepartmentService;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.*;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public final class TeacherDao {
    private static TeacherDao teacherDao = new TeacherDao();


    private TeacherDao() {
    }

    public static TeacherDao getInstance() {
        return teacherDao;
    }

    public static Collection<Teacher> findAll(List conditionList, Pagination pagination, Connection connection) throws SQLException {
        //创建集合类对象，用来保存所有记录代表的Teacher对象
        Collection<Teacher> teachers = new TreeSet<Teacher>();
        //准备预编译的语句
        int totalNum = CommonDao.getInstance().count(connection,"teacher");
        //创建查询的主语句
        StringBuilder sql_select = new StringBuilder("SELECT * FROM teacher ");
        //如果有条件，则生成条件子句，并附加到主语句后面
        if(conditionList!=null){
            String whereClause = Condition.toWhereClause(conditionList);
            sql_select.append(whereClause);
        }
        if(pagination!=null){
            String LimitClause = pagination.toLimitClause(totalNum);
            sql_select.append(LimitClause);
        }
        //执行预编译语句，结果集放在resultSet中
        PreparedStatement pstmt = connection.prepareStatement(sql_select.toString());
        ResultSet resultSet = pstmt.executeQuery();
        //遍历resultSet
        while(resultSet.next()){
            teachers.add(new Teacher(resultSet.getInt("id"),
                    resultSet.getString("name"),
                    resultSet.getString("no")
                    ));
        }
        return teachers;
    }

    public static Teacher find(int id ,Connection connection) throws SQLException {

        //创建sql语句
        String find_sql = "select * from teacher where id = ?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(find_sql);
        //为预编译参数赋值
        pstmt.setInt(1, id);
        //创建结果集对象
        ResultSet resultSet = pstmt.executeQuery();
        //游标下移
        resultSet.next();
        //创建Teacher对象，各字段为结果集中相应字段
        Teacher teacher = new Teacher(resultSet.getInt("id"),
                resultSet.getString("name"),
                resultSet.getString("no")
        );
        return teacher;
    }
    public static boolean update(Teacher teacher,Connection connection) throws SQLException {

        //创建sql语句
        String updateSchool_sql = "update teacher set name=?,no=?,department_id=? where id=?";
        //在该链接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(updateSchool_sql);
        //为预编译参数赋值
        pstmt.setInt(4,teacher.getId());
        pstmt.setString(1,teacher.getName());
        pstmt.setString(2,teacher.getNo());
        pstmt.setInt(3,teacher.getDepartment().getId());
        //根据执行结果，具体判断所要返回的值
        if (pstmt.executeUpdate()==0){
            return false;
        }else
            return  true;
    }

    public void add(Teacher teacher, Connection connection) throws SQLException {
        //根据连接对象准备语句对象，如果SQL语句为多行，注意语句不同部分之间要有空格
        PreparedStatement preparedStatement =
                connection.prepareStatement(
                        "INSERT INTO teacher " +
                                " (id, name,no)" +
                                " VALUES (?, ?, ?,?,?,?)");
        preparedStatement.setInt(1, teacher.getId());
        preparedStatement.setString(2, teacher.getName());
        preparedStatement.setString(3, teacher.getNo());
        preparedStatement.setInt(4, teacher.getTitle().getId());
        preparedStatement.setInt(5, teacher.getDegree().getId());
        preparedStatement.setInt(6, teacher.getDepartment().getId());
        preparedStatement.executeUpdate();
        //本方法是事务的一部分，不能释放资源
        JdbcHelper.close(preparedStatement,null);
    }
    public static boolean delete(int id,Connection connection) throws SQLException{

        //创建sql语句
        String deleteTeacher_sql = "DELETE FROM teacher WHERE id=?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(deleteTeacher_sql);
        //为预编译参数赋值
        pstmt.setInt(1,id);
        if (pstmt.executeLargeUpdate() == 1){
            return true;
        }else {
            return false;
        }
    }
    public static boolean delete(Teacher teacher,Connection connection) throws SQLException {
        String deleteTeacher_sql = "delete from teacher where no = ?";
        //在连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(deleteTeacher_sql );
        //为预编译参数赋值
        pstmt.setString(1,teacher.getNo());
        //判断是否删除成功
        if(pstmt.executeUpdate() == 0){
            return false;
        }else {
            return true;
        }
    }
    public static int countAll(Department department) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        PreparedStatement pstmt = connection.prepareStatement("SELECT COUNT(id) as cnt_by_department FROM teacher WHERE department_id=?");
        pstmt.setInt(1,department.getId());
        ResultSet resultSet = pstmt.executeQuery();
        resultSet.next();
        int result = resultSet.getInt("cnt_by_department");
        JdbcHelper.close(resultSet,pstmt,connection);
        return result;
    }
}